const CONFIG = require('../../config.js');

let TEXTData = {};
Page({
	data: {
		backUrl: '/pages/index/index',
		navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#39a9ed))',
		loginInfo:'',
		proTitle: '',
		proLists: [
			{
				name: '基本信息',
				code: 'A',
				isShow: true,
				isSave: false,
			},  {
				name: '企业信息',
				code: 'B',
				isShow: true,
				isSave: false
			}, {
				name: '工作信息',
				code: 'C',
				isShow: true,
				isSave: false
			}, {
				name: '房屋信息',
				code: 'HouseList',
				isShow: true,
				isSave: false,
				open: true,
			}, {
				name: '车辆信息',
				code: 'CarList',
				isShow: true,
				isSave: false,
				open: true
			}, {
				name: '保单信息',
				code: 'InsuranceList',
				isShow: true,
				isSave: false,
				open: true
			}, {
				name: TEXTData.zhengxinxinxi,
				code: 'E',
				isShow: true,
				isSave: false
			},  {
				name: TEXTData.fzxx,
				code: 'F',
				isShow: true,
				isSave: false
			}, {
				name: '流水信息',
				code: 'G',
				isShow: true,
				isSave: false
			}, {
				name: '法律信息',
				code: 'H',
				isShow: true,
				isSave: false
			}
		],
		showlists:{
			HouseList: false,
			CarList: false,
			InsuranceList: false,
		},
		baseInfos: {
			houseList:[],
			carList: [],
			insuranceList: [],
		},
		isShowB: true,
		houseChecks: [],
		carChecks: [],
		insuranceChecks: [],
		productTypeid: '',
		productTypePid: '',
		houstList: [],
		titleData: {},
		isMarry: 1,
		isWork: 1,
	},

	// checkbox
	changeCheck(e){
		let { index } = e.currentTarget.dataset;//这里可以知道被改变的复选框的index
		const {field} = e.currentTarget.dataset;
		this.setData({
			[`${field}[${index}]`]: !this.data[field][index]
		});
	},

	// 展开房屋，车辆，保单 信息
	openFn(e){
		let idx = e.currentTarget.dataset.value;
		this.setData({
			['proLists[' + idx + '].open']: !this.data.proLists[idx].open
		})
	},

	// 房屋，车辆，保单 新增、编辑
	addListFn(e){
		let code = e.currentTarget.dataset.code;
		let proId = e.currentTarget.dataset.proid || '';
		wx.navigateTo({
			url: '/pages/productEdit/edit' + code + '/index?productTypeid=' + this.data.productTypeid + '&proId=' + proId + '&isMarry=' + this.data.isMarry + '&isWork='+ this.data.isWork,
		})
	},

	// 删除房屋，车辆，保单 列表
	delFn(e){
		let {code, proid} = e.currentTarget.dataset;
		let that = this;
		wx.showModal({
			title: '提示',
			content: '确定要删除吗？',
			success: function (sm) {
			  	if (sm.confirm) {
				  	// 用户点击了确定 可以调用删除方法了
					let params = { id: proid, type: '' }
					if(code === 'HouseList'){
						params.type = '0';
					}else if(code === 'CarList'){
						params.type = '1';
					}else if(code === 'InsuranceList'){
						params.type = '2';
					}
					that.delData(params);
				} else if (sm.cancel) {
				  	console.log('用户点击取消')
				}
			}
		})
	},

	// 删除数据
	delData(params){
		wx.request({
			url: CONFIG.host + "/system/appInfoController/deleteCustomerAttachInfo",
			data: params,
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					wx.showToast({
						title: res.data.message,
						icon: 'success',
						duration: 2000
					})
					setTimeout(()=>{
						this.getProductInfoFn();
					}, 1000)
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 获取产品信息填写情况,判断是否跳转到 基本信息编辑页
	getProductInfoFn(){
		var that = this;
		wx.showToast({title: '加载中', icon: 'loading', duration: 10000});
		wx.request({
			url: CONFIG.host + "/system/appInfoController/customerCompleteProject",
			data: {},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				this.getBaseInfo();
				if(res.data.code === 'success'){
					// 车辆选中
					let datas = res.data.data;
					let houseCheck = [],carCheck = [],insuranceCheck = [];
					datas.carList.forEach((item,index) => {
						carCheck.push( true );
					})
					datas.houseList.forEach((item,index) => {
						houseCheck.push( true );
					})
					datas.insuranceList.forEach((item,index) => {
						insuranceCheck.push( true );
					})
					this.setData({
						baseInfos: datas,
						houseChecks: houseCheck,
						carChecks: carCheck,
						insuranceChecks: insuranceCheck,
					})
					if(datas && datas.A === 0){
						wx.redirectTo({
							url: '/pages/productEdit/editA/index?productTypeid=' + this.data.productTypeid,
						})
					}
				}
				wx.hideToast();
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	
	setProInfo(){
		let baseInfos = this.data.baseInfos;
		for(let key in baseInfos){
			this.data.proLists.forEach(item => {
				let code = item.code.toLowerCase();
				let mykey = key.toLowerCase();
				if(code === mykey){
					if(this.data.productTypeid === '10001' || this.data.productTypeid === '10002'){   // 房屋
						if (item.code === 'C' || item.code === 'CarList' || item.code === 'InsuranceList'){
							item.isShow = false;
						}
					}else if(this.data.productTypeid === '20001' || this.data.productTypeid === '20002'){ // 企业贷
						if (item.code === 'C' || item.code === 'HouseList' || item.code === 'CarList' || item.code === 'InsuranceList'){
							item.isShow = false;
						}
					}else if(this.data.productTypeid === '40001'){ // 车辆
						if (item.code === 'B' || 
							item.code === 'C' || 
							item.code === 'G' ||
							item.code === 'H' || 
							item.code === 'HouseList' || 
							item.code === 'InsuranceList'){
							item.isShow = false;
						}
					}else if(this.data.productTypeid === '30001'){ // 按揭房
						if (item.code === 'F' || 
							item.code === 'CarList' || 
							item.code === 'InsuranceList'){
							item.isShow = false;
						}
					}else if(this.data.productTypeid === '30002' || this.data.productTypeid === '30006'){ // 公积金贷款,打卡工资信用贷
						if (item.code === 'F' || 
							item.code === 'B' || 
							item.code === 'HouseList' || 
							item.code === 'CarList' || 
							item.code === 'InsuranceList'){
							item.isShow = false;
						}
					}else if(this.data.productTypeid === '30004'){ // 按揭车
						if (item.code === 'F' || 
							item.code === 'G' || 
							item.code === 'H' || 
							item.code === 'HouseList' || 
							item.code === 'InsuranceList'){
							item.isShow = false;
						}
					}else if(this.data.productTypeid === '30005'){ // 保单贷
                        if (item.code === 'F' ||
                            item.code === 'HouseList' ||
                            item.code === 'CarList'){
                            item.isShow = false;
                        }
                    }
					if(typeof baseInfos[key] ==='object'){
						item.isSave = !!baseInfos[key].length;
						item.proList = baseInfos[key];
					}else{
						item.isSave = !!baseInfos[key];
					}
				}
			});
		}
		this.setData({
			proLists: this.data.proLists
		})
	},
	// 获取基本信息
	getBaseInfo(){
		let that = this;
		wx.request({
			url: CONFIG.host + "/system/appInfoController/getCustomerProductDetailInfo",
			data: {
				itemType: 'A',
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					let data = res.data.data;
					let isWork , isMarry;
					for (let i = 0; i < data.length; i++) {
						if (data[i].itemCode === 'A009') {
							isWork = data[i].itemValue;
							that.data.proLists.forEach((item, index) => {
								if(item.code === 'B'){
									if(data[i].itemValue === '1' || data[i].itemValue === '3'){
										item.isShow = false;
									}else{
										item.isShow = true;
									}
								}else if(item.code === 'C'){
									if(data[i].itemValue === '2' || data[i].itemValue === '3'){
										item.isShow = false;
									}else{
										item.isShow = true;
									}
								}
							})
						}else if(data[i].itemCode === 'A003'){
							isMarry = data[i].itemValue;
						}
					}
					that.setData({
						proLists: that.data.proLists,
						isMarry: isMarry,
						isWork: isWork
					})
					this.setProInfo();
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

    unlockFn(e){
        let { id } = e.currentTarget.dataset;
        let that = this;
        wx.showModal({
            title: '提示',
            content: '解锁将消耗100积分,确定要解锁吗？',
            success: function (sm) {
                if (sm.confirm) {
                    that.setUnlock(id);
                } else if (sm.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
		},
		
	// 一键匹配
	matchBtn(){
        let that = this;
        wx.showModal({
            title: '郑重承诺',
            showCancel: false,
            content: TEXTData.bcxzx,
            success: function(res) {
                if (res.confirm) {
                    that.matchNew();
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
	},

	matchNew(){
            let houseIds = [], carIds = [], insuranceIds  = [];
            let baseInfos = this.data.baseInfos;
            for(let i=0;i< baseInfos.houseList.length; i++){
                if(this.data.houseChecks[i]){
                    houseIds.push(baseInfos.houseList[i].houseId)
                }
            }
            for(let i=0;i< baseInfos.carList.length; i++){
                if(this.data.carChecks[i]){
                    carIds.push(baseInfos.carList[i].carId)
                }
            }
            for(let i=0;i< baseInfos.insuranceList.length; i++){
                if(this.data.insuranceChecks[i]){
                    insuranceIds.push(baseInfos.insuranceList[i].carId)
                }
            }
            let params = {
                carIds: carIds.join(','),
                houseIds: houseIds.join(','),
                insuranceIds: insuranceIds.join(','),
                productTypeId: this.data.productTypeid,
                productTypePid: this.data.productTypePid,
            }
            wx.request({
                url: CONFIG.host + "system/appInfoController/matchProduct",
                data: params,
                method: "GET",
                header: {
                    'content-type': 'application/json',
                    'Authorization': this.data.loginInfo.token
                },
                success: res => {
                if(res.data.code === 'success'){
                wx.showToast({
                    title: res.data.message,
                    icon: 'success',
                    duration: 2000
                })
                setTimeout(()=>{
                    wx.switchTab({
                    url: '/pages/plan/index'
                })
            }, 1000)
            }else if(res.data.code === 'error'){
                wx.showToast({
                    title: res.data.message,
                    icon: 'error',
                    duration: 2000
                })
            }
        },fail: function (error) {
                wx.showToast({
                    title: error,
                    icon: 'none',
                })
            }
        })
	},

	matchText(){
		let titleData = { titleName: '', markText: '' };
		switch(this.data.productTypeid){
			case '10001': 
				titleData = { titleName: TEXTData.fxdy1, markText: TEXTData.markText1 }
				break;
			case '10002': 
				titleData = { titleName: TEXTData.fxdy2, markText: TEXTData.markText2 }
				break;
			case '20001': 
				titleData = { titleName: TEXTData.qy1, markText: TEXTData.markText3 }
				break;
			case '20002': 
				titleData = { titleName: TEXTData.qy2, markText: TEXTData.markText4}
				break;
			case '30001': 
				titleData = { titleName: TEXTData.xy1, markText: TEXTData.markText5 }
				break;
			case '30002': 
				titleData = { titleName: TEXTData.xy2, markText: TEXTData.markText6 }
				break;
			case '30004': 
				titleData = { titleName: TEXTData.xy4, markText: TEXTData.markText7 }
				break;
			case '30005': 
				titleData = { titleName: TEXTData.xy5, markText: TEXTData.markText8 }
				break;
			case '30006': 
				titleData = { titleName: TEXTData.xy6, markText: TEXTData.markText9 }
				break;
			case '40001': 
				titleData = { titleName: TEXTData.cl1, markText: TEXTData.markText10 }
				break;
		}
		this.setData({
			titleData: titleData
		})
	},

	onLoad: function (options) {
			let showNum = wx.getStorageSync('showUser') === '0' ? '0' : '1';
			TEXTData = require('../../utils/text'+ showNum +'.js');
			TEXTData = TEXTData.textJson;
			let loginInfo = wx.getStorageSync('loginInfo') || '';
			if(loginInfo){
				this.setData({
					loginInfo: JSON.parse(loginInfo),
					[`proLists[6].name`]: TEXTData.zhengxinxinxi,
					[`proLists[7].name`]: TEXTData.fzxx,
				})
			}
			let that = this;
			wx.getStorage({
				key:"productInfo",
				success (res) {
					let data = JSON.parse(res.data);
					that.setData({
						productTypePid: data.productTypePid,
						productTypeid: data.productTypeid,
					})
					that.matchText();
				}
			})
	},
	  
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.getProductInfoFn();
	},
})
